import Vue from "vue";
import VueRouter from "vue-router";

import Home from "../views/home/home.vue";
import Login from "../views/home/login.vue";

import ExamineeList from "../views/examinee/examineeList.vue";
import ExamineeFrom from "../views/examinee/examineeFrom.vue";

import GradeList from "../views/enroll/gradeList.vue";
import CheckEnroll from "../views/enroll/checkEnroll.vue";
import AddUserInforList from "../views/enroll/addUserInforList.vue";
import ConfirmOrder from "../views/enroll/confirmOrder.vue";
import PaymentOrder from "../views/enroll/paymentOrder.vue";
import EnrollDetail from "../views/enroll/enrollDetail.vue";

import OrderList from "../views/order/orderList.vue";
import PersonalOrderList from "../views/order/personalOrderList.vue";
import PersonalOrderDetail from "../views/order/personalOrderDetail.vue";

import ResultsList from "../views/results/resultsList.vue";

import UserEdit from "../views/user/userEdit.vue";

Vue.use(VueRouter);

const routes = [
  // home
  {
    path: "/",
    name: "Home",
    component: Home,
    meta: {
      title: "首页",
    },
  }, // 首页
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      title: "登录",
    },
  }, // 登录

  // 考生管理
  {
    path: "/examinee/examineeList",
    name: "ExamineeList",
    component: ExamineeList,
    meta: {
      title: "考生列表",
    },
  }, // 考生列表
  {
    path: "/examinee/examineeFrom",
    name: "ExamineeFrom",
    component: ExamineeFrom,
    meta: {
      title: "考生管理",
    },
  }, // 添加/编辑考生

  // 考试报名
  {
    path: "/enroll/gradeList",
    name: "GradeList",
    component: GradeList,
    meta: {
      title: "考试报名",
    },
  }, // 选择等级
  {
    path: "/enroll/checkEnroll",
    name: "CheckEnroll",
    component: CheckEnroll,
    meta: {
      title: "考试报名",
    },
  }, // 选择报名
  {
    path: "/enroll/addUserInforList",
    name: "AddUserInforList",
    component: AddUserInforList,
    meta: {
      title: "考试报名",
    },
  }, // 填写报名信息
  {
    path: "/enroll/confirmOrder",
    name: "ConfirmOrder",
    component: ConfirmOrder,
    meta: {
      title: "考试报名",
    },
  }, // 确认报名
  {
    path: "/enroll/paymentOrder",
    name: "PaymentOrder",
    component: PaymentOrder,
    meta: {
      title: "考试报名",
    },
  }, // 支付费用
  {
    path: "/enroll/enrollDetail",
    name: "EnrollDetail",
    component: EnrollDetail,
    meta: {
      title: "报名详情",
    },
  }, // 报名详情

  // 机构订单
  {
    path: "/order/orderList",
    name: "OrderList",
    component: OrderList,
    meta: {
      title: "机构订单",
    },
  }, // 机构订单
  {
    path: "/order/personalOrderList",
    name: "PersonalOrderList",
    component: PersonalOrderList,
    meta: {
      title: "个人订单",
    },
  }, // 个人订单
  {
    path: "/order/personalOrderList/personalOrderDetail",
    name: "PersonalOrderDetail",
    component: PersonalOrderDetail,
    meta: {
      title: "订单详情",
    },
  }, // 个人订单详情

  // 成绩查询
  {
    path: "/results/resultsList",
    name: "ResultsList",
    component: ResultsList,
    meta: {
      title: "成绩查询",
    },
  }, // 成绩查询

  // 账号管理
  {
    path: "/user/userEdit",
    name: "UserEdit",
    component: UserEdit,
    meta: {
      title: "账号信息",
    },
  }, // 账号信息
];

const router = new VueRouter({
  mode: "history",
  base: "/team/",
  routes,
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 需要登录的页面
  const nextRoute = [
    "Home",

    "ExamineeList",
    "ExamineeFrom",

    "GradeList",
    "CheckEnroll",
    "AddUserInforList",
    "ConfirmOrder",
    "PaymentOrder",
    "EnrollDetail",

    "OrderList",
    "PersonalOrderList",
    "PersonalOrderDetail",

    "ResultsList",
    "UserEdit",
  ];
  // const nextRoute = [];
  let isLogin = localStorage.getItem("TEAM_LOGIN_TOKEN"); // 判断是否登录，本地存储有用户数据则视为已经登录
  // 未登录状态；当路由到 nextRoute 指定页时，跳转至 登录
  if (nextRoute.indexOf(to.name) >= 0) {
    // 检测是否登录的页面
    if (!isLogin) {
      // 如果未登录（本地存储无用户数据），并且要跳到登录页面
      if (from.name === "Login") {
        next("/login");
        return;
      }
      // 登录后，跳到到当前页面
      router.push({
        name: "Login",
        params: {
          redirect: to.fullPath,
        },
      });
    }
  }

  // 已登录状态；当路由到 登录 时，跳转至 首页
  if (to.name === "Login") {
    if (isLogin) {
      next("/");
      return;
    }
  }
  next(); // 必须使用 next ,执行效果依赖 next 方法的调用参数
});
export default router;
